package com.nulldev.util.networking.npacket4.iprotocols.ncryptx;

import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;

import com.nulldev.util.networking.npacket4.nPacket;
import com.nulldev.util.networking.npacket4.extensions.PacketExtension;
import com.nulldev.util.security.encryptAPI.Encryptors.SystemEncryptors;

public class nCryptCipherInformation extends nPacket {

	private static final long serialVersionUID = 4675262677953062921L;
	private SystemEncryptors[] ciphers;
	private byte cryptVersion = nCryptX.CRYPT_VERSION;

	public nCryptCipherInformation(final nPacket packet) {
		super("ncryptx.cipher_info", packet);
	}

	public nCryptCipherInformation(final SystemEncryptors[] ciphers, final PacketExtension... exts) {
		super("ncryptx.cipher_info", exts);
		this.ciphers = ciphers;
	}

	@Override
	public void onGenerate(DataOutputStream dos) throws IOException {
		dos.writeByte(this.cryptVersion);
		dos.writeInt(this.ciphers.length);
		for (final SystemEncryptors cipher : this.ciphers)
			dos.writeInt(cipher.cipherID());
	}

	@Override
	public void onPacket(DataInputStream dis) throws IOException {
		this.cryptVersion = dis.readByte();
		final int cipherCount = dis.readInt();
		final SystemEncryptors[] ciphers = new SystemEncryptors[cipherCount];
		for (int i = 0; i < cipherCount; i++)
			ciphers[i] = SystemEncryptors.get(dis.readInt());
		this.ciphers = ciphers;
	}

	public SystemEncryptors[] getSupportedCiphers() {
		return this.ciphers;
	}

	public byte getCryptVersion() {
		return this.cryptVersion;
	}

}
